package hutech.ksv.view;

import hutech.ksv.entity.ThongTinTaiKhoanAdmin;
import hutech.ksv.service.AdminAccountService;

import java.io.IOException;
import java.io.Serializable;

import javax.faces.context.FacesContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component
@Scope("session")
public class AdminLoginManagedBean implements Serializable {

	/**
	 * 
	 */
	
	//Cac properties
	private static final long serialVersionUID = 1L;

	private String username;
	private String password;	
	private ThongTinTaiKhoanAdmin thongTinTaiKhoanAdmin;	
	
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public ThongTinTaiKhoanAdmin getThongTinTaiKhoanAdmin() {
		return thongTinTaiKhoanAdmin;
	}
	public void setThongTinTaiKhoanAdmin(ThongTinTaiKhoanAdmin thongTinTaiKhoanAdmin) {
		this.thongTinTaiKhoanAdmin = thongTinTaiKhoanAdmin;
	}
	
	@Autowired
	AdminAccountService adminAccountService;	
	
	public String login() {
		thongTinTaiKhoanAdmin = adminAccountService.checkLogin(username, password);
		
		if(thongTinTaiKhoanAdmin != null) {
			return "welcomeAdmin";
		}		
		return "adminDangNhapLoi";
	}
	
	public void onLoad() {
		if(thongTinTaiKhoanAdmin != null) {
			try {
				FacesContext.getCurrentInstance().getExternalContext().redirect("welcomeAdmin.xhtml");
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println(e.getMessage());
			}
		}
	}
	
	public String logout() {
		thongTinTaiKhoanAdmin = null;
		return "AdminLogin.xhtml";
	}
	
}
